package chapter07

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

/**
 * 数组、可变数组
 *
 */
object Test02_ArrayBuffer {
  def main(args: Array[String]): Unit = {
    //TODO 1、创建可变数组
    println("--------TODO 1、创建可变数组-----------------")
    val arr1: ArrayBuffer[Int] = new ArrayBuffer[Int]()
    val arr2 = ArrayBuffer(23, 44, 66, 77, 31)

    println(arr1)
    println(arr1.mkString(","))
    println(arr2)

    println("--------TODO 2、访问元素-----------------")
    //TODO 2、访问元素
    println(arr2(1))
    arr2(1) = 39
    println(arr2(1))

    //    arr1(1) = 1
    //    println(arr1(1))

    println("-------TODO 3、添加元素-------------------------")

    val newArr1 = arr1 :+ 15
    println(arr1)
    println(newArr1)
    println(newArr1 == arr1)

    // +=相当于追加了
    val newArr2 = arr1 += 19
    println(arr1)
    println(newArr2)
    println(newArr2 == arr1)
    newArr2 += 13
    println(arr1)

    //往前追加 +：
    77 +=: arr1
    println(arr1)
    println(newArr2)

    arr1.append(36)
    arr1.prepend(11, 34)
    arr1.insert(1, 35, 59)
    println(arr1)

    arr1.insertAll(2, newArr1)
    arr1.prependAll(newArr2)

    println(arr1)
    println("-------TODO 4、删除元素-------------------")
    arr1.remove(3)
    println(arr1)

    arr1.remove(0, 10)
    println(arr1)

    //寻找删除、找不到什么都不做
    arr1 -= 13
    println(arr1)

    println("-------TODO 5、可变数组转不可变数组-------------------")
    val arr: ArrayBuffer[Int] = ArrayBuffer(23, 56, 46)
    val newArray: Array[Int] = arr.toArray
    println(newArray.mkString(","))
    println(arr)

    println("-------------不可变数组转换为可变数组---------------")
    val buffer: mutable.Buffer[Int] = newArray.toBuffer
    println(buffer)
    println(newArray)
  }

}
